{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "sgGS_yECtOYL"
   },
   "source": [
    "# 1、LSTM+CNN+RNN调参"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "iH6wnVahtYTf"
   },
   "source": [
    "## （1）准备数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 283
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 6192,
     "status": "ok",
     "timestamp": 1597396650929,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "6KsCSxCJbNv2",
    "outputId": "94f6f74f-b39f-42d6-f0c6-4190b4ecf388"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2020-08-14 09:17:38--  https://github.com/kumarvinit15/Data-Science-Capstone/archive/master.zip\n",
      "Resolving github.com (github.com)... 140.82.114.3\n",
      "Connecting to github.com (github.com)|140.82.114.3|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://codeload.github.com/kumarvinit15/Data-Science-Capstone/zip/master [following]\n",
      "--2020-08-14 09:17:38--  https://codeload.github.com/kumarvinit15/Data-Science-Capstone/zip/master\n",
      "Resolving codeload.github.com (codeload.github.com)... 140.82.112.10\n",
      "Connecting to codeload.github.com (codeload.github.com)|140.82.112.10|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: unspecified [application/zip]\n",
      "Saving to: ‘master.zip’\n",
      "\n",
      "\rmaster.zip              [<=>                 ]       0  --.-KB/s               \rmaster.zip              [ <=>                ]   1.83M  --.-KB/s    in 0.1s    \n",
      "\n",
      "2020-08-14 09:17:38 (17.1 MB/s) - ‘master.zip’ saved [1921556]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://github.com/kumarvinit15/Data-Science-Capstone/archive/master.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 150
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 3824,
     "status": "ok",
     "timestamp": 1597396701406,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "ioN2gaLybRyh",
    "outputId": "fdc6eee2-4869-4b05-ceea-e22347e6d86b"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Archive:  master.zip\n",
      "0c007d4e0d08cd4c6e6e92aca2dc4d2ba20877d8\n",
      "   creating: Data-Science-Capstone-master/\n",
      "  inflating: Data-Science-Capstone-master/DS Capstone.ipynb  \n",
      "  inflating: Data-Science-Capstone-master/README.md  \n",
      "  inflating: Data-Science-Capstone-master/sample_submission.csv  \n",
      "  inflating: Data-Science-Capstone-master/test.csv  \n",
      "  inflating: Data-Science-Capstone-master/train.csv  \n"
     ]
    }
   ],
   "source": [
    "!unzip master.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 100
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 11438,
     "status": "ok",
     "timestamp": 1597374441714,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "FRQ3JxaUs-TM",
    "outputId": "4ab5e64a-2aa5-40d9-9381-f43eb1313a32"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: emoji in /usr/local/lib/python3.6/dist-packages (0.6.0)\n",
      "Requirement already satisfied: fasttext in /usr/local/lib/python3.6/dist-packages (0.9.2)\n",
      "Requirement already satisfied: setuptools>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from fasttext) (49.2.0)\n",
      "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from fasttext) (1.18.5)\n",
      "Requirement already satisfied: pybind11>=2.2 in /usr/local/lib/python3.6/dist-packages (from fasttext) (2.5.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install emoji\n",
    "!pip install fasttext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 120
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 77679,
     "status": "ok",
     "timestamp": 1597396621616,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "6UgGXdv99L_-",
    "outputId": "80c90f36-faf4-4463-d78b-87f1c111a966"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly&response_type=code\n",
      "\n",
      "Enter your authorization code:\n",
      "··········\n",
      "Mounted at /content/gdrive\n"
     ]
    }
   ],
   "source": [
    "from google.colab import drive  \n",
    "drive.mount('/content/gdrive') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_kg_hide-input": false,
    "_kg_hide-output": true,
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 103
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 4906,
     "status": "ok",
     "timestamp": 1597374462178,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "MAG4hLW-bDu9",
    "outputId": "3004236c-54fe-4adf-b6dd-d0b3b7d5ef81"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[nltk_data] Downloading package punkt to /root/nltk_data...\n",
      "[nltk_data]   Unzipping tokenizers/punkt.zip.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
      "  import pandas.util.testing as tm\n"
     ]
    }
   ],
   "source": [
    "import numpy as np \n",
    "import pandas as pd \n",
    "from tensorflow.keras.layers import Dense, Input\n",
    "from tensorflow.keras.optimizers import Adam\n",
    "from tensorflow.keras.models import Model\n",
    "from tensorflow.keras.callbacks import ModelCheckpoint\n",
    "import tensorflow_hub as hub\n",
    "import emoji\n",
    "import os\n",
    "import time\n",
    "import sys\n",
    "import fasttext\n",
    "import re\n",
    "import nltk \n",
    "nltk.download('punkt')\n",
    "from nltk.corpus import stopwords\n",
    "import tensorflow as tf\n",
    "tf.keras.backend.clear_session()\n",
    "\n",
    "import itertools\n",
    "import collections\n",
    "from collections import Counter\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from wordcloud import WordCloud\n",
    "#load data \n",
    "for dirname, _, filenames in os.walk('/kaggle/input'):\n",
    "    for filename in filenames:\n",
    "        print(os.path.join(dirname, filename))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0",
    "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a",
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 216
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1282,
     "status": "ok",
     "timestamp": 1597397023339,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "tiFv3AkUbDvE",
    "outputId": "a699340e-9110-404f-f8fc-fae63fa73d8a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       True\n",
      "1       True\n",
      "2       True\n",
      "3       True\n",
      "4       True\n",
      "        ... \n",
      "7608    True\n",
      "7609    True\n",
      "7610    True\n",
      "7611    True\n",
      "7612    True\n",
      "Name: keyword, Length: 7613, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd \n",
    "train = pd.read_csv(\"Data-Science-Capstone-master/train.csv\")\n",
    "train.head()\n",
    "print(train['keyword'].isna())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 197
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 2201,
     "status": "ok",
     "timestamp": 1597374472928,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "zmlO1BX_bDvK",
    "outputId": "5d984e34-325a-4995-f3e9-67d4d0f7b65b"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>keyword</th>\n",
       "      <th>location</th>\n",
       "      <th>text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Just happened a terrible car crash</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Heard about #earthquake is different cities, s...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>there is a forest fire at spot pond, geese are...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Apocalypse lighting. #Spokane #wildfires</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Typhoon Soudelor kills 28 in China and Taiwan</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id keyword location                                               text\n",
       "0   0     NaN      NaN                 Just happened a terrible car crash\n",
       "1   2     NaN      NaN  Heard about #earthquake is different cities, s...\n",
       "2   3     NaN      NaN  there is a forest fire at spot pond, geese are...\n",
       "3   9     NaN      NaN           Apocalypse lighting. #Spokane #wildfires\n",
       "4  11     NaN      NaN      Typhoon Soudelor kills 28 in China and Taiwan"
      ]
     },
     "execution_count": 20,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_csv(\"Data-Science-Capstone-master/test.csv\")\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "7IOP8zWcbDvT"
   },
   "outputs": [],
   "source": [
    "train=train[['text','target']]\n",
    "test=test[['id','text']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 83
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1619,
     "status": "ok",
     "timestamp": 1597374479911,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "k30izGp2bDvY",
    "outputId": "b311de87-14ea-42c7-bc9b-1d68244ba2eb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "target\n",
       "0    4342\n",
       "1    3271\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {
      "tags": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check class distribution in train dataset\n",
    "from scipy import stats\n",
    "train.groupby(['target']).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "caC4EycSNfP-"
   },
   "outputs": [],
   "source": [
    "x_train = list(train['text'])  # 转化为列表的形式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EflM5iPrbDvq"
   },
   "source": [
    "## （2）分词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "w0DRSJyO6hPs"
   },
   "outputs": [],
   "source": [
    "from keras.preprocessing.text import Tokenizer\n",
    "from keras.preprocessing.sequence import pad_sequences\n",
    "\n",
    "\n",
    "tokenizer = Tokenizer(num_words=10000)\n",
    "tokenizer.fit_on_texts(x_train)\n",
    "sequences = tokenizer.texts_to_sequences(x_train)\n",
    "word_index = tokenizer.word_index\n",
    "data = pad_sequences(sequences, maxlen=100)\n",
    "\n",
    "labels = np.asarray(train['target'])  # 预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "lxZpr2JXYjdn"
   },
   "outputs": [],
   "source": [
    "training_samples = 5200\n",
    "\n",
    "x_train = data[:training_samples]\n",
    "y_train = labels[:training_samples]\n",
    "x_val = data[training_samples: ]\n",
    "y_val = labels[training_samples: ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "fKfc9-tPOa3U"
   },
   "source": [
    "## （3）下载Glove词嵌入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 350
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 392311,
     "status": "ok",
     "timestamp": 1597374900001,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "ECSBKVlqOhiI",
    "outputId": "3d528728-eaa1-4718-eb7b-102d55876d20"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2020-08-14 03:08:42--  http://nlp.stanford.edu/data/wordvecs/glove.6B.zip\n",
      "Resolving nlp.stanford.edu (nlp.stanford.edu)... 171.64.67.140\n",
      "Connecting to nlp.stanford.edu (nlp.stanford.edu)|171.64.67.140|:80... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://nlp.stanford.edu/data/wordvecs/glove.6B.zip [following]\n",
      "--2020-08-14 03:08:42--  https://nlp.stanford.edu/data/wordvecs/glove.6B.zip\n",
      "Connecting to nlp.stanford.edu (nlp.stanford.edu)|171.64.67.140|:443... connected.\n",
      "HTTP request sent, awaiting response... 301 Moved Permanently\n",
      "Location: http://downloads.cs.stanford.edu/nlp/data/wordvecs/glove.6B.zip [following]\n",
      "--2020-08-14 03:08:42--  http://downloads.cs.stanford.edu/nlp/data/wordvecs/glove.6B.zip\n",
      "Resolving downloads.cs.stanford.edu (downloads.cs.stanford.edu)... 171.64.64.22\n",
      "Connecting to downloads.cs.stanford.edu (downloads.cs.stanford.edu)|171.64.64.22|:80... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 862182753 (822M) [application/zip]\n",
      "Saving to: ‘glove.6B.zip’\n",
      "\n",
      "glove.6B.zip        100%[===================>] 822.24M  2.11MB/s    in 6m 27s  \n",
      "\n",
      "2020-08-14 03:15:09 (2.13 MB/s) - ‘glove.6B.zip’ saved [862182753/862182753]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget http://nlp.stanford.edu/data/wordvecs/glove.6B.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 100
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 25946,
     "status": "ok",
     "timestamp": 1597374971221,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "PX0NDd22Vaii",
    "outputId": "cc38c9fa-fc59-4d2a-8f72-2fc40640761c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Archive:  glove.6B.zip\n",
      "  inflating: glove.6B.100d.txt       \n",
      "  inflating: glove.6B.200d.txt       \n",
      "  inflating: glove.6B.300d.txt       \n",
      "  inflating: glove.6B.50d.txt        \n"
     ]
    }
   ],
   "source": [
    "!unzip glove.6B.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "dUkw4gjgTwvl"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "glove_dir = ''\n",
    "\n",
    "embeddings_index = {}\n",
    "f = open(os.path.join(glove_dir, 'glove.6B.100d.txt'))\n",
    "for line in f:\n",
    "    values = line.split()\n",
    "    word = values[0]\n",
    "    coefs = np.asarray(values[1:], dtype='float32')\n",
    "    embeddings_index[word] = coefs\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Tosyevp2XH4F"
   },
   "outputs": [],
   "source": [
    "embedding_dim = 100  # 这里的维度必须为100\n",
    "\n",
    "embedding_matrix = np.zeros((10000, embedding_dim))\n",
    "for word, i in word_index.items():\n",
    "    embedding_vector = embeddings_index.get(word)\n",
    "    if i < 100:\n",
    "        if embedding_vector is not None:\n",
    "            embedding_matrix[i] = embedding_vector  # 嵌入索引（embeddings_index） 中找不到的词，其嵌入向量全为 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "zc6hdPqVgMz9"
   },
   "source": [
    "## （4）模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 383
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 7223,
     "status": "ok",
     "timestamp": 1597375094526,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "IBX6WTmMWqJF",
    "outputId": "a6344ca6-cc2d-4d40-97bb-b8f20fc51651"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "embedding (Embedding)        (None, 100, 100)          1000000   \n",
      "_________________________________________________________________\n",
      "lstm (LSTM)                  (None, 64)                42240     \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 32)                2080      \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                330       \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 5)                 55        \n",
      "_________________________________________________________________\n",
      "dense_4 (Dense)              (None, 1)                 6         \n",
      "=================================================================\n",
      "Total params: 1,048,871\n",
      "Trainable params: 1,048,871\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Embedding, Flatten, Dense\n",
    "from keras.layers import LSTM\n",
    "from keras.layers import SimpleRNN\n",
    "\n",
    "model = Sequential()\n",
    "model.add(Embedding(10000, embedding_dim, input_length=100))\n",
    "model.add(LSTM(64))\n",
    "model.add(Dense(64, activation='relu'))\n",
    "model.add(Dense(32, activation='relu'))\n",
    "model.add(Dense(10, activation='relu'))\n",
    "model.add(Dense(5, activation='relu'))\n",
    "model.add(Dense(1, activation='sigmoid'))\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "qoTWEsi7WyuU"
   },
   "outputs": [],
   "source": [
    "model.layers[0].set_weights([embedding_matrix])\n",
    "model.layers[0].trainable = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 116607,
     "status": "ok",
     "timestamp": 1597375249437,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "MYINpLRxX6oB",
    "outputId": "664442c5-d982-4d35-fac6-2fca98f8a82e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "147/147 [==============================] - 1s 10ms/step - loss: 0.6366 - acc: 0.6415 - val_loss: 0.6278 - val_acc: 0.6712\n",
      "Epoch 2/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.6017 - acc: 0.6850 - val_loss: 0.6186 - val_acc: 0.6538\n",
      "Epoch 3/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5866 - acc: 0.6985 - val_loss: 0.6453 - val_acc: 0.6712\n",
      "Epoch 4/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5753 - acc: 0.7058 - val_loss: 0.6620 - val_acc: 0.6423\n",
      "Epoch 5/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5660 - acc: 0.7111 - val_loss: 0.6205 - val_acc: 0.6538\n",
      "Epoch 6/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5602 - acc: 0.7128 - val_loss: 0.6939 - val_acc: 0.6519\n",
      "Epoch 7/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5549 - acc: 0.7175 - val_loss: 0.6362 - val_acc: 0.6904\n",
      "Epoch 8/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5495 - acc: 0.7248 - val_loss: 0.6453 - val_acc: 0.6865\n",
      "Epoch 9/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5450 - acc: 0.7295 - val_loss: 0.6382 - val_acc: 0.6692\n",
      "Epoch 10/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5386 - acc: 0.7329 - val_loss: 0.6217 - val_acc: 0.6692\n",
      "Epoch 11/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5338 - acc: 0.7355 - val_loss: 0.6396 - val_acc: 0.6846\n",
      "Epoch 12/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5275 - acc: 0.7359 - val_loss: 0.6262 - val_acc: 0.6962\n",
      "Epoch 13/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5192 - acc: 0.7357 - val_loss: 0.6812 - val_acc: 0.6404\n",
      "Epoch 14/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5115 - acc: 0.7474 - val_loss: 0.6375 - val_acc: 0.6788\n",
      "Epoch 15/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.5040 - acc: 0.7494 - val_loss: 0.6490 - val_acc: 0.6865\n",
      "Epoch 16/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4948 - acc: 0.7553 - val_loss: 0.7088 - val_acc: 0.6827\n",
      "Epoch 17/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4894 - acc: 0.7545 - val_loss: 0.6848 - val_acc: 0.6731\n",
      "Epoch 18/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4812 - acc: 0.7641 - val_loss: 0.7064 - val_acc: 0.6769\n",
      "Epoch 19/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4693 - acc: 0.7679 - val_loss: 0.6822 - val_acc: 0.6538\n",
      "Epoch 20/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4610 - acc: 0.7754 - val_loss: 0.7051 - val_acc: 0.6827\n",
      "Epoch 21/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4514 - acc: 0.7748 - val_loss: 0.7922 - val_acc: 0.6654\n",
      "Epoch 22/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4433 - acc: 0.7748 - val_loss: 0.7343 - val_acc: 0.6673\n",
      "Epoch 23/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4313 - acc: 0.7878 - val_loss: 0.7794 - val_acc: 0.6442\n",
      "Epoch 24/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4229 - acc: 0.7906 - val_loss: 0.7809 - val_acc: 0.6769\n",
      "Epoch 25/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4119 - acc: 0.7979 - val_loss: 0.7763 - val_acc: 0.6519\n",
      "Epoch 26/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.4050 - acc: 0.7976 - val_loss: 0.8399 - val_acc: 0.6615\n",
      "Epoch 27/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3966 - acc: 0.8045 - val_loss: 0.9080 - val_acc: 0.5865\n",
      "Epoch 28/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3819 - acc: 0.8066 - val_loss: 0.9084 - val_acc: 0.6712\n",
      "Epoch 29/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3706 - acc: 0.8197 - val_loss: 0.8448 - val_acc: 0.6538\n",
      "Epoch 30/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3592 - acc: 0.8237 - val_loss: 1.0802 - val_acc: 0.6250\n",
      "Epoch 31/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3491 - acc: 0.8318 - val_loss: 1.0029 - val_acc: 0.6288\n",
      "Epoch 32/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3440 - acc: 0.8280 - val_loss: 0.9753 - val_acc: 0.6173\n",
      "Epoch 33/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3292 - acc: 0.8404 - val_loss: 0.9795 - val_acc: 0.6346\n",
      "Epoch 34/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3200 - acc: 0.8457 - val_loss: 1.1352 - val_acc: 0.6577\n",
      "Epoch 35/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3124 - acc: 0.8498 - val_loss: 1.0402 - val_acc: 0.6500\n",
      "Epoch 36/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.3013 - acc: 0.8566 - val_loss: 1.4888 - val_acc: 0.6346\n",
      "Epoch 37/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2955 - acc: 0.8613 - val_loss: 1.1844 - val_acc: 0.6154\n",
      "Epoch 38/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2901 - acc: 0.8632 - val_loss: 1.1900 - val_acc: 0.6577\n",
      "Epoch 39/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2770 - acc: 0.8647 - val_loss: 1.2606 - val_acc: 0.6404\n",
      "Epoch 40/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2665 - acc: 0.8748 - val_loss: 1.5520 - val_acc: 0.6308\n",
      "Epoch 41/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2614 - acc: 0.8814 - val_loss: 1.2740 - val_acc: 0.6231\n",
      "Epoch 42/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2547 - acc: 0.8814 - val_loss: 1.4291 - val_acc: 0.6250\n",
      "Epoch 43/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2468 - acc: 0.8863 - val_loss: 1.3540 - val_acc: 0.6615\n",
      "Epoch 44/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2362 - acc: 0.8893 - val_loss: 1.4332 - val_acc: 0.6231\n",
      "Epoch 45/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2341 - acc: 0.8900 - val_loss: 1.4410 - val_acc: 0.6212\n",
      "Epoch 46/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2046 - acc: 0.9062 - val_loss: 1.6889 - val_acc: 0.6058\n",
      "Epoch 52/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.2007 - acc: 0.9098 - val_loss: 1.6317 - val_acc: 0.6173\n",
      "Epoch 53/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1985 - acc: 0.9122 - val_loss: 1.6866 - val_acc: 0.6019\n",
      "Epoch 54/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1885 - acc: 0.9145 - val_loss: 1.7966 - val_acc: 0.5981\n",
      "Epoch 55/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1823 - acc: 0.9124 - val_loss: 1.6904 - val_acc: 0.5981\n",
      "Epoch 56/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1851 - acc: 0.9147 - val_loss: 1.7113 - val_acc: 0.5904\n",
      "Epoch 57/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1820 - acc: 0.9156 - val_loss: 2.1068 - val_acc: 0.6058\n",
      "Epoch 58/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1751 - acc: 0.9175 - val_loss: 1.9532 - val_acc: 0.6019\n",
      "Epoch 59/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1710 - acc: 0.9212 - val_loss: 1.9245 - val_acc: 0.6115\n",
      "Epoch 60/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1653 - acc: 0.9244 - val_loss: 1.9497 - val_acc: 0.6000\n",
      "Epoch 61/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1738 - acc: 0.9209 - val_loss: 1.9534 - val_acc: 0.6154\n",
      "Epoch 62/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1643 - acc: 0.9259 - val_loss: 2.1485 - val_acc: 0.6231\n",
      "Epoch 63/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1666 - acc: 0.9246 - val_loss: 2.0405 - val_acc: 0.5923\n",
      "Epoch 64/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1593 - acc: 0.9256 - val_loss: 2.1612 - val_acc: 0.5750\n",
      "Epoch 65/100\n",
      "147/147 [==============================] - 1s 8ms/step - loss: 0.1548 - acc: 0.9274 - val_loss: 1.9167 - val_acc: 0.6096\n",
      "Epoch 66/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1583 - acc: 0.9303 - val_loss: 2.2694 - val_acc: 0.5942\n",
      "Epoch 67/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1557 - acc: 0.9278 - val_loss: 2.1564 - val_acc: 0.6096\n",
      "Epoch 68/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1488 - acc: 0.9284 - val_loss: 1.9512 - val_acc: 0.6058\n",
      "Epoch 69/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1532 - acc: 0.9274 - val_loss: 2.2069 - val_acc: 0.5808\n",
      "Epoch 70/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1467 - acc: 0.9318 - val_loss: 2.3597 - val_acc: 0.5750\n",
      "Epoch 71/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1446 - acc: 0.9327 - val_loss: 2.2839 - val_acc: 0.5808\n",
      "Epoch 72/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1431 - acc: 0.9323 - val_loss: 2.2481 - val_acc: 0.6135\n",
      "Epoch 73/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1471 - acc: 0.9308 - val_loss: 2.1665 - val_acc: 0.5827\n",
      "Epoch 74/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1394 - acc: 0.9327 - val_loss: 2.4356 - val_acc: 0.5865\n",
      "Epoch 75/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1365 - acc: 0.9318 - val_loss: 2.5431 - val_acc: 0.6115\n",
      "Epoch 76/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1389 - acc: 0.9365 - val_loss: 2.6369 - val_acc: 0.6038\n",
      "Epoch 77/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1395 - acc: 0.9353 - val_loss: 2.2094 - val_acc: 0.6192\n",
      "Epoch 78/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1353 - acc: 0.9350 - val_loss: 2.5831 - val_acc: 0.6231\n",
      "Epoch 79/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1320 - acc: 0.9357 - val_loss: 2.9723 - val_acc: 0.5615\n",
      "Epoch 80/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1350 - acc: 0.9370 - val_loss: 2.1357 - val_acc: 0.6154\n",
      "Epoch 81/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1308 - acc: 0.9363 - val_loss: 2.8395 - val_acc: 0.5654\n",
      "Epoch 82/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1327 - acc: 0.9350 - val_loss: 2.5038 - val_acc: 0.6038\n",
      "Epoch 83/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1290 - acc: 0.9365 - val_loss: 2.6100 - val_acc: 0.6058\n",
      "Epoch 84/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1318 - acc: 0.9365 - val_loss: 2.7153 - val_acc: 0.5788\n",
      "Epoch 85/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1276 - acc: 0.9410 - val_loss: 2.7922 - val_acc: 0.6192\n",
      "Epoch 86/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1210 - acc: 0.9363 - val_loss: 2.7735 - val_acc: 0.6077\n",
      "Epoch 87/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1252 - acc: 0.9402 - val_loss: 2.8052 - val_acc: 0.6212\n",
      "Epoch 88/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1191 - acc: 0.9419 - val_loss: 2.9909 - val_acc: 0.6096\n",
      "Epoch 89/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1251 - acc: 0.9406 - val_loss: 3.0014 - val_acc: 0.5827\n",
      "Epoch 90/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1237 - acc: 0.9419 - val_loss: 2.6043 - val_acc: 0.6077\n",
      "Epoch 91/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1186 - acc: 0.9429 - val_loss: 2.6846 - val_acc: 0.5885\n",
      "Epoch 92/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1251 - acc: 0.9372 - val_loss: 2.6857 - val_acc: 0.6019\n",
      "Epoch 93/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1161 - acc: 0.9429 - val_loss: 2.7267 - val_acc: 0.5885\n",
      "Epoch 94/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1246 - acc: 0.9408 - val_loss: 2.9646 - val_acc: 0.6096\n",
      "Epoch 95/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1206 - acc: 0.9400 - val_loss: 2.9320 - val_acc: 0.6038\n",
      "Epoch 96/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1164 - acc: 0.9419 - val_loss: 3.5341 - val_acc: 0.6096\n",
      "Epoch 97/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1192 - acc: 0.9402 - val_loss: 2.9641 - val_acc: 0.5962\n",
      "Epoch 98/100\n",
      "147/147 [==============================] - 1s 7ms/step - loss: 0.1204 - acc: 0.9387 - val_loss: 2.8349 - val_acc: 0.6288\n",
      "Epoch 99/100\n",
      "147/147 [==============================] - 1s 8ms/step - loss: 0.1178 - acc: 0.9436 - val_loss: 2.9124 - val_acc: 0.6192\n",
      "Epoch 100/100\n",
      "147/147 [==============================] - 1s 8ms/step - loss: 0.1174 - acc: 0.9444 - val_loss: 2.9615 - val_acc: 0.5808\n"
     ]
    }
   ],
   "source": [
    "model.compile(optimizer='rmsprop',\n",
    "              loss='binary_crossentropy',\n",
    "              metrics=['acc'])\n",
    "history = model.fit(x_train, y_train,\n",
    "                    epochs=100,\n",
    "                    batch_size=32,\n",
    "                    validation_split=0.1)\n",
    "model.save_weights('pre_trained_glove_model.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "D_8ojb6VhN0S"
   },
   "source": [
    "## （5）评价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "pRGjrn5ni_eU"
   },
   "outputs": [],
   "source": [
    "test = pd.read_csv(\"Data-Science-Capstone-master/train.csv\")\n",
    "test = test[['text','target']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "sv7lxd69krg1"
   },
   "outputs": [],
   "source": [
    "tokenizer = Tokenizer(num_words=10000)\n",
    "tokenizer.fit_on_texts(test['text'])\n",
    "sequences = tokenizer.texts_to_sequences(test['text'])\n",
    "word_index_2 = tokenizer.word_index\n",
    "x_test = pad_sequences(sequences, maxlen=100)\n",
    "\n",
    "y_test = np.asarray(test['target'])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 66
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 3011,
     "status": "ok",
     "timestamp": 1597319908074,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "dKcBN-0whQaC",
    "outputId": "e4eed1d8-dd0e-46f9-8b7e-38dd692d1cd4"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "238/238 [==============================] - 1s 3ms/step - loss: 1.9067 - acc: 0.8365\n",
      "损失1.9067246913909912\n",
      "测试集精度0.8364639282226562\n"
     ]
    }
   ],
   "source": [
    "test_loss, test_acc = model.evaluate(x_test, y_test)\n",
    "print('损失'+str(test_loss))\n",
    "print('测试集精度'+str(test_acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "IpgrYax5B2c8"
   },
   "outputs": [],
   "source": [
    "pre = pd.read_csv(\"Data-Science-Capstone-master/test.csv\")\n",
    "\n",
    "pre = pre[['text']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "9Woq-FSKDR1d"
   },
   "outputs": [],
   "source": [
    "tokenizer = Tokenizer(num_words=10000)\n",
    "tokenizer.fit_on_texts(pre['text'])\n",
    "sequences = tokenizer.texts_to_sequences(pre['text'])\n",
    "word_index_2 = tokenizer.word_index\n",
    "x_pre = pad_sequences(sequences, maxlen=100)\n",
    "\n",
    "# y_test = np.asarray(test['target'])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 133
    },
    "colab_type": "code",
    "executionInfo": {
     "elapsed": 1457,
     "status": "ok",
     "timestamp": 1597377851173,
     "user": {
      "displayName": "songshou dong",
      "photoUrl": "",
      "userId": "04969514503852022851"
     },
     "user_tz": -480
    },
    "id": "zSadkTm7Dj1Q",
    "outputId": "ff962db1-97b9-480d-9994-696e7bb1e517"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.]\n",
      " [1.]\n",
      " [0.]\n",
      " ...\n",
      " [0.]\n",
      " [0.]\n",
      " [1.]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "y = model.predict(x_pre)\n",
    "y=np.arrray(y)\n",
    "for i in range(0, len(y)):\n",
    "  if y[i]>=0.5:\n",
    "    y[i]=int('1')\n",
    "  else:\n",
    "    y[i]=int('0')\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "9ZJXvRB6Etzy"
   },
   "outputs": [],
   "source": [
    "sub=pd.read_csv(\"Data-Science-Capstone-master/sample_submission.csv\")\n",
    "sub['target']=y\n",
    "#print(sub)\n",
    "sub.head()\n",
    "sub.to_csv(\"sub.csv\",index=False)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "machine_shape": "hm",
   "name": "CNN+RNN+LSTM.ipynb",
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
